package util;

import com.obs.services.ObsClient;
import com.obs.services.model.ObsObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.PropertyResourceBundle;

/**
 * @program: sijiacheng-obs-use
 * @create: 2023-06-29
 * 下载对象
 **/
public class OBSDownloadFile {
    public static String ak="";
    public static String sk="";
    public static String endPoint="";
    public static void main(String[] args) throws Exception {
        //读取resources内properties配置文件中的秘钥信息, 该段根据自己需要, 写死也可以 add by sijiacheng 20230811 start
        try {
            InputStream inputStream = ClassLoader.getSystemResourceAsStream("huaweicloud.properties");
            PropertyResourceBundle bundle = new PropertyResourceBundle(inputStream);
            ak=bundle.getString("ak");
            sk=bundle.getString("sk");
            endPoint=bundle.getString("endPoint");
        } catch (IOException e) {
            System.out.println("从resources内properties配置文件中,获取密钥失败。");
            throw new RuntimeException(e);
        }
        /*// Endpoint请按实际情况填写。
        String endPoint = "https://obs.cn-east-3.myhuaweicloud.com";
        String ak = "UEYW*******KSV";
        String sk = "PnXLi************9su5IkhDRB";*/
        //读取resources内properties配置文件中的秘钥信息, 该段根据自己需要, 写死也可以 add by sijiacheng 20230811 end

        // 创建ObsClient实例
        final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

        ObsObject obsObject = obsClient.getObject("sjcobs", "网络延时说明.xls");
        // 读取对象内容
        System.out.println("Object content:");
        InputStream input = obsObject.getObjectContent();
        byte[] b = new byte[1024];
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int len;
        while ((len=input.read(b)) != -1){
            bos.write(b, 0, len);
        }

        System.out.println(new String(bos.toByteArray(),"UTF-8"));
        bos.close();
        input.close();

    }
}
